<?

//查出邀请人 下级邀请人 及自己所在联盟的fid
//以及是哪个联盟哪个省市的代理商
//return  $id_case;

$admin_account =array('admin','123666');
if(in_array($_SESSION['saccount'],$admin_account))
    goto buttom;
$sel_inviterID = "select id from pft_member where inviterID={$_SESSION['sid']}";
$GLOBALS['le']->query($sel_inviterID);
while($row[] = $GLOBALS['le']->fetch_assoc()){}
array_pop($row); 

if(count($row)){
    foreach($row as $v){
        $mids_1 .= $v['id'].',';
    }
}
$mids_1 = substr( $mids_1, 0, -1);

if($mids_1!=''){
    //查询被邀请人是否加入联盟的情况
    $sel_inviter_union = "select fid from pft_union_member_info_SE where memberID in ($mids_1)";
    $GLOBALS['le']->query($sel_inviter_union);
    unset($row);
    while($row[] = $GLOBALS['le']->fetch_assoc()){}
    array_pop($row); 
    $member_ids = array();
    if($count_1 = count($row)){
        foreach($row as $v){
            $fids[$v['fid']] = $v['fid'];
            $id_case['inviterID'][$v['fid']] = $v['fid'];            
            if(!in_array($v['fid'],$member_ids))
                $member_ids[] = $v['fid'];
        }                   
        foreach($id_case['inviterID'] as $v){
                $tmp = $v.',';
        }        
        $id_case['inviterID'] = substr( $tmp, 0, -1);
        unset($tmp);
    }
    
    //是否有下级被邀请人
    $sel_P_inviterID = "select id from pft_member where inviterID in($mids_1)";
    $GLOBALS['le']->query($sel_P_inviterID);
    unset($row);
    while($row[] = $GLOBALS['le']->fetch_assoc()){}
    array_pop($row); 

    foreach($row as $v){
        $mids_2 .= $v['id'].',';
    }
    $mids_2 = substr( $mids_2, 0, -1);

    //查询下级被邀请人是否加入联盟的情况
    if($mids_2!=''){
        $sel_p_inviter_union = "select fid from pft_union_member_info_SE where memberID in ($mids_2)";
        $GLOBALS['le']->query($sel_p_inviter_union);
        unset($row);
        while($row[] = $GLOBALS['le']->fetch_assoc()){}
        array_pop($row); 
        if($count_2 = count($row)){
            foreach($row as $v){
                $id_case['P_inviterID'][$v['fid']] = $v['fid'];
                $fids[$v['fid']] = $v['fid'];
                if(!in_array($v['fid'],$member_ids))
                    $member_ids[] = $v['fid'];
            }                 
            foreach($id_case['P_inviterID'] as $v){
                $tmp = $v.',';
            }                   
            $id_case['P_inviterID']=substr( $tmp, 0, -1);
            unset($tmp);
        }
    }
}

//盟主
if($_SESSION['alliance']!='' ){
    $fids[$_SESSION['sid']] = $id_case['fid'] = $_SESSION['sid'];
    
}

//加入哪些联盟
$sel_union_fid = "SELECT fid from pft_union_member_info_SE where memberID in ({$_SESSION['sid']}) ";
$GLOBALS['le']->query($sel_union_fid);
unset($row);
while($row[] = $GLOBALS['le']->fetch_assoc()){
}
array_pop($row); 

if(count($row)){
    foreach($row as $v){
        $fids[$v['fid']] = $v['fid'];        
        $id_case['member'] .= $v['fid'].',';
    }
    $id_case['member'] = substr( $id_case['member'], 0, -1);
}

/*
if(($count_1 +$count_2)==0 && $_SESSION['alliance']==''){
    echo "<script>alert('您和您的邀请人还没有加入任何分销联盟');
                window.history.back(-1);     
            </script>";
	exit;
}
*/
if($id_case['member'] =='' && $id_case['fid'] ==''){
    echo "<script>alert('您还没有加入任何分销联盟');
                window.history.back(-1);     
            </script>";
	exit;
}

//在哪些联盟是代理商
$sel_agency = "SELECT fid,darea_code from pft_distributor_agency where inviterID={$_SESSION['sid']} and dstatus=0 ";
$GLOBALS['le']->query($sel_agency);
unset($row);
while($row[] = $GLOBALS['le']->fetch_assoc()){
}
array_pop($row);


if(count($row)){
    foreach($row as $v){
        if( strlen( $v['darea_code'] ) ==3){//省代理商情况
            $id_case['agencyID_P'] .= $v['fid'].',';
        }else if( strlen( $v['darea_code'] ) ==4){//市 直辖市代理商情况
            $id_case['agencyID_C'] .= $v['fid'].',';
        }
    }
   if($id_case['agencyID_P'])
        $id_case['agencyID_P'] = substr( $id_case['agencyID_P'], 0, -1); 
   if($id_case['agencyID_C'])
        $id_case['agencyID_C'] = substr( $id_case['agencyID_C'], 0, -1); 
}

foreach($fids as $v){
    $fid_tmp .= $v.',';
}
$fid = substr($fid_tmp, 0, -1);
buttom:

